﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWeb.ServiceBus.Common;
using MyWeb.ServiceBus.Model;
using MyWeb.ServiceBus.Model.Dto;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services;

namespace MyWeb.ServiceBus.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ApplicationController : ControllerBase
    {
        private IApplicationService _service;
        public ApplicationController(IApplicationService service)
        {
            _service = service;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var app = await _service.QueryPageAsync(new ApplicationDto());
            return Ok(app);
        }
        [HttpGet]
        public async Task<ApiResult> GetPage(int page, int pageSize)
        {
            var request = new ApplicationDto()
            {
                Pagination = new PageModel()
                {
                    Page = page,
                    PageSize = pageSize
                }
            };
            // 执行分页查询
            var apps = await _service.QueryPageAsync(request);

            var pageData = new Model.PageModel(page, request.Pagination.DataCount, pageSize, apps);
            return ApiResult.SUCCESS(res: pageData);
        }

        [HttpPost]
        public async Task<ApiResult> Add(ApplicationDto app)
        {
            var com = await _service.AddAsync(app);
            return com >= 1 ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        [HttpPut]
        public async Task<ApiResult> Edit(ApplicationDto app)
        {
            app.UpdateDate = DateTime.Now;
            var com = await _service.EditAsync(app);
            return com >= 1 ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
        [HttpDelete]
        public async Task<ApiResult> Remove(long id)
        {
            var com = await _service.DeleteAsync(id);
            return com >= 1 ? ApiResult.SUCCESS() : ApiResult.FAILED();
        }
    }
}
